<?php

namespace app\index\controller;

use app\index\controller\Main;
use think\Controller;
use think\Db;

class Agent extends Main {

    public function index() {

        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 1);
        $map = array('ismerch' => 1);
        if ($this->sysset['agent_level'] != '') {
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 0) {
                $map['agent_id'] = $this->merchinfo['id'];
            }
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 1) {
                $map['province'] = $this->merchinfo['province'];
                $map['city'] = $this->merchinfo['city'];
                $map['area'] = $this->merchinfo['area'];
                $map['agent_id'] = $this->merchinfo['id'];
            }
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 2) {
                $map['province'] = $this->merchinfo['province'];
                $map['city'] = $this->merchinfo['city'];
                $map['agent_id'] = $this->merchinfo['id'];
            }
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 3) {
                $map['province'] = $this->merchinfo['province'];
                $map['agent_id'] = $this->merchinfo['id'];
            }
        } else {
            $map['agent_id'] = $this->merchinfo['id'];
        }
        if (!empty($_GPC['id'])) {
            $map['id'] = $_GPC['id'];
        }
      
        if (isset($_GPC['gender']) && $_GPC['gender'] != '') {
            $map['gender'] = $_GPC['gender'];
        }
        
        $db = Db::name('member')->where($map);
        
          if (!empty($_GPC['nickname'])) {
           $db->whereLike('nickname','%' . $_GPC['nickname'] . '%');
        }
        if (!empty($_GPC['openid'])) {
            $db->whereLike('openid','%' . $_GPC['openid'] . '%');
        }
        if (!empty($_GPC['mobile'])) {
            $db->whereLike('mobile','%' . $_GPC['mobile'] . '%');
        }
        
        $count = $db->count();
        $data = $db->order('createtime DESC')
                        ->limit(($page - 1) * $limit, $limit)->select();
        jsonp($data, $count);
    }

    public function add() {

        $_GPC = input();
        if ($this->request->isPost()) {
            $token = check_formtoken();
              unset($_GPC['data']['token']);
            $isupdate = false;
            $_GPC['data']['ismerch'] = 1;
            $_GPC['data']['agent_id'] = $this->merchinfo['id'];
            $_GPC['data']['createtime'] = time();
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            list($msec, $sec) = explode(' ', microtime());
            $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
            if (!isset($_GPC['data']['pwd'] ) || $_GPC['data']['pwd'] == '') {
                $_GPC['data']['pwd'] = md5('123456' . $msectime);
            } elseif(isset($_GPC['data']['pwd'] ) && $_GPC['data']['pwd'] != ''){
                $_GPC['data']['pwd'] = md5($_GPC['data']['pwd'] . $msectime);
            }
            $result = pdo_get('member', array('openid' => $_GPC['data']['openid']));
            if (!empty($result) || $result === 0) {
                if ($result['ismerch'] == '1') {
                    json(array('token' => $token), 1, '商户已存在！');
                }
                if ($_GPC['data']['avatar'] == '/upload/avatar.png') {
                    unset($_GPC['data']['avatar']);
                }
                if ($_GPC['data']['nickname'] == '') {
                    unset($_GPC['data']['nickname']);
                }
                if ($this->merchinfo['isagent'] == 1) {
                    $agent = pdo_get('member', array('id' => $this->merchinfo['id']));
                    if ($agent['num'] != 0 && $_GPC['data']['num'] == 0) {
                        json('', 1, '点数必须大于0！');
                    }
                    if ($agent['num'] != 0) {
                        $isupdate = true;
                        if ($agent['num'] == -1) {
                            $agent['num'] = 0;
                        }
                        $agent['num'] += $result['num'];
                        if ($agent['num'] < $_GPC['data']['num']) {
                            json(array('token' => $token), 1, '您的代理点数剩余：' . $agent['num'] . '，点数最多只能填写：' . $agent['num'] . '！');
                        }
                    }
                }
                $result = pdo_update('member', $_GPC['data'], array('id' => $result['id']));
            } else {
                if ($this->merchinfo['isagent'] == 1) {
                    $agent = pdo_get('member', array('id' => $this->merchinfo['id']));
                    if ($agent['num'] != 0 && $_GPC['data']['num'] == 0) {
                        json('', 1, '点数必须大于0！');
                    }
                    if ($agent['num'] != 0) {
                        $isupdate = true;
                        if ($agent['num'] == -1) {
                            $agent['num'] = 0;
                        }
                        if ($agent['num'] < $_GPC['data']['num']) {
                            json(array('token' => $token), 1, '您的代理点数剩余：' . $agent['num'] . '，点数最多只能填写：' . $agent['num'] . '！');
                        }
                    }
                }
                $result = pdo_insert('member', $_GPC['data']);
            }
            if (!empty($result) || $result === 0) {
                if ($this->merchinfo['isagent'] == 1 && $isupdate) {
                    $agent['num'] -= $_GPC['data']['num'];
                    if ($agent['num'] == 0) {
                        $agent['num'] = -1;
                    }
                    pdo_update('member', $agent, array('id' => $agent['id']));
                }
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        $data = array('avatar' => '/upload/avatar.png');
        jsont(get_upload_data($data));
    }

    public function edit() {

        $_GPC = input();
        $id = $_GPC['id'];
        $data = pdo_get('member', array('id' => $id));
        if ($this->request->isPost()) {
            $token = check_formtoken();
             unset($_GPC['data']['token']);
            $isupdate = false;
            if ($this->merchinfo['isagent'] == 1 && $this->merchinfo['id'] != $id) {
                $agent = pdo_get('member', array('id' => $this->merchinfo['id']));
                if ($agent['num'] != 0 && $_GPC['data']['num'] == 0) {
                    json('', 1, '点数必须大于0！');
                }
                if ($agent['num'] != 0) {
                    $isupdate = true;
                    if ($agent['num'] == -1) {
                        $agent['num'] = 0;
                    }
                    $agent['num'] += $data['num'];
                    if ($agent['num'] < $_GPC['data']['num']) {
                        json(array('token' => $token), 1, '您的代理点数剩余：' . $agent['num'] . '，点数最多只能填写：' . $agent['num'] . '！');
                    }
                }
            }
            $_GPC['data']['ismerch'] = 1;
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            if ($this->merchinfo['id'] == $id) {
                $agent = pdo_get('member', array('id' => $this->merchinfo['id']));
                $_GPC['data']['num'] = $agent['num'];
                $_GPC['data']['sale_num'] = $agent['sale_num'];
                $_GPC['data']['endtime'] = $agent['endtime'];
            }
            if ($_GPC['data']['pwd'] == '') {
                unset($_GPC['data']['pwd']);
            } else {
                list($msec, $sec) = explode(' ', microtime());
                $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
                $_GPC['data']['pwd'] = md5($_GPC['data']['pwd'] . $msectime);
                $_GPC['data']['salt'] = $msectime;
            }
            $result = pdo_update('member', $_GPC['data'], array('id' => $id));
            if (!empty($result) || $result === 0) {
                if ($this->merchinfo['isagent'] == 1 && $this->merchinfo['id'] != $id && $isupdate) {
                    $agent['num'] -= $_GPC['data']['num'];
                    if ($agent['num'] == 0) {
                        $agent['num'] = -1;
                    }
                    pdo_update('member', $agent, array('id' => $agent['id']));
                }
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        $data['endtime'] = date('Y-m-d', $data['endtime']);
        jsont(get_upload_data($data));
    }

    public function del() {

        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            $result = Db::name('member')->whereIn('id', $id)->update(['ismerch' => 0]);
            if (!empty($result) || $result === 0) {
                json('success', 0, '删除成功！');
            } else {
                json('error', 1, '删除失败！');
            }
        }
        json('', 1, 'error');
    }

}
